package com.marui.ch1.vola;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * violate无法提供操作的原子性的解决方案（atomicInteger）
 * @author marui
 * @date 2020/4/9 9:28
 * @since 1.0
 */
public class VolatileUnsafe2 {

	AtomicInteger atomicInteger = new AtomicInteger();


    public static void main(String[] args) throws InterruptedException {
		VolatileUnsafe2 volatileUnsafe = new VolatileUnsafe2();
		for (int i = 0; i < 20000; i++) {
			new Thread(() -> {
				for (int j = 0; j < 20; j++) {
					volatileUnsafe.atomicInteger.getAndIncrement();
				}
			}).start();
		}

		Thread.sleep(3000);

		System.out.println("a is "+volatileUnsafe.atomicInteger);
	}

}
